package cn.lili.controller.grab;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.DateUtil;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.grab.entity.dos.GoodsGrab;
import cn.lili.modules.grab.entity.dto.GoodsGrabReqParam;
import cn.lili.modules.grab.alibaba.service.GoodsGrabService;
import cn.lili.modules.store.client.StoreClient;
import cn.lili.modules.store.entity.dos.Store;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 商品管理接口
 *
 * @author pikachu
 * @since 2020-02-23 15:18:56
 */
@Slf4j
@RestController
@Api(tags = "商品采集接口")
@RequiredArgsConstructor
@RequestMapping("/goods/grab")
public class GoodsGrabController {

    private final StoreClient storeClient;

    private final GoodsGrabService goodsGrabService;

    @ApiOperation(value = "开始商品采集")
    @PostMapping(value = "/start")
    public ResultMessage<String> startGrab(@RequestBody GoodsGrabReqParam goodsGrabReqParam) {
        try {
            // 读取选中的列表
            String result = goodsGrabService.goodsGrab(goodsGrabReqParam);
            return ResultUtil.data(result);
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.GOODS_ERROR.message(), e);
            return ResultUtil.error(ResultCode.GOODS_ERROR);
        }
    }



    @ApiOperation(value = "保存采集记录")
    @PostMapping(value = "/save")
    public ResultMessage<String> saveGrab(@RequestBody GoodsGrab goodsGrab) {
        try {
            String userId = UserContext.getCurrentUser().getExtendId();
            if(UserContext.getCurrentUser().getScene() != SceneEnums.SUPPLIER){
                return ResultUtil.error(ResultCode.GOODS_GRAB_ERROR);
            }
            //判断当前用户是否允许采集
            Store store = storeClient.getStore(userId);
            if(null == store){
                return ResultUtil.error(ResultCode.USER_NOT_EXIST);
            }
            if(!store.getGrabAlibabaOpen()){
                return ResultUtil.error(ResultCode.GOODS_GRAB_ERROR);
            }

            goodsGrab.setSiteId(userId);
            goodsGrab.setCreateTime(new Date().getTime());
            goodsGrabService.save(goodsGrab);
            return ResultUtil.data(goodsGrab.getGrabId());
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.GOODS_ERROR.message(), e);
            return ResultUtil.error(ResultCode.GOODS_ERROR);
        }
    }



    @ApiOperation(value = "获取采集记录")
    @GetMapping(value = "/getById/{grabId}")
    public ResultMessage<GoodsGrab> getById(@PathVariable String grabId) {
        try {
            GoodsGrab one = goodsGrabService.getById(grabId);
            if(null == one){
                return ResultUtil.error(ResultCode.GOODS_GRAB_ERROR);
            }
            one.setTimeStr(DateUtil.toString(one.getCreateTime()));
            return ResultUtil.data(one);
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.GOODS_ERROR.message(), e);
            return ResultUtil.error(ResultCode.GOODS_ERROR);
        }
    }


}
